package entityJPA;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * The persistent class for the interview database table.
 * 
 */
@Entity
@Table(name = "interview")
@NamedQueries({
		@NamedQuery(name = "Interview.findAll", query = "SELECT i FROM Interview i"),
		@NamedQuery(name = "Interview.findByIdCandidate", query = "SELECT i FROM Interview i join i.candidate c where c.idCandidate=:id"), })
public class Interview implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(unique = true, nullable = false)
	private int idInterview;

	@Temporal(TemporalType.DATE)
	@Column(nullable = false)
	private Date date;

	// bi-directional many-to-one association to Candidate
	@ManyToOne
	@JoinColumn(name = "idCandidate", nullable = false)
	private Candidate candidate;

	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name = "Interviewuser", joinColumns = { @JoinColumn(name = "idInterview", referencedColumnName = "idInterview") }, inverseJoinColumns = { @JoinColumn(name = "idUser", referencedColumnName = "idUser") })
	private List<User> users;

	public Interview() {
	}

	public int getIdInterview() {
		return this.idInterview;
	}

	public void setIdInterview(int idInterview) {
		this.idInterview = idInterview;
	}

	public Date getDate() {
		return this.date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Candidate getCandidate() {
		return this.candidate;
	}

	public void setCandidate(Candidate candidate) {
		this.candidate = candidate;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String usersToString() {

		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < users.size() - 1; i++) {
			sb.append(users.get(i).getFirstName());
			sb.append(" ");
			sb.append(users.get(i).getLastName());
			sb.append("; ");
		}
			sb.append(users.get(users.size() - 1).getFirstName());
			sb.append(" ");
			sb.append(users.get(users.size() - 1).getLastName());
		

		return sb.toString();

	}

}